package com.woniuxy.tfss.web.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.tfss.entity.*;
import com.woniuxy.tfss.service.DiscountService;
import com.woniuxy.tfss.service.UdService;
import com.woniuxy.tfss.service.UserService;
import com.woniuxy.tfss.utils.MyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Controller("adminUserController")
@RequestMapping("admin/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UdService udService;
    @Autowired
    private DiscountService discountService;


    /**
     * 查询所有用户
     * @return
     */
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity<List<User>> list(){
        List<User> users=userService.list();
        return new ResponseEntity<>(users);
    }

    /**
     * 用户多条件分页查询
     * @param account
     * @param discountIds
     * @param regTime
     * @param status
     * @param page
     * @return
     */
    @GetMapping("listByPage")
    @ResponseBody
    public ResponseEntity<Page<User>> listByPage(String account, Integer[] discountIds, String[] regTime,String status, Integer page){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        QueryWrapper<Ud> wrapper1=new QueryWrapper<>();
        if(!StringUtils.isEmpty(account)){
            wrapper.like("account",account);
        }else if(!StringUtils.isEmpty((status))&& !"0".equals(status)){
            wrapper.eq("status",status);
        }else if(discountIds!=null && discountIds.length!=0){
            for(Integer discountId : discountIds){
                wrapper1.or().eq("discountId",discountId);
            }
            List<Ud> uds =udService.list(wrapper1);
            for(Ud ud :uds){
                wrapper.or().eq("id",ud.getUserId());
            }
        }else if(regTime.length!=0){
            wrapper.between("regTime",regTime[0],regTime[1]);
        }
        Page userPage=new Page(page==null?1:page,3);
        return new ResponseEntity<>(userService.page(userPage,wrapper));
    }

    /**
     * 冻结用户
     * @param id
     * @return
     */
    @GetMapping("changeStatus")
    @ResponseBody
    public  ResponseEntity<Void> changeStatus(Integer id){
        User user=userService.getById(id);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        if(User.NORMAL.equals(user.getStatus())){
            user.setStatus(Admin.LOCKED);
        }else{
            user.setStatus(Admin.NOMAL);
        }
        userService.update(user,wrapper);
        return ResponseEntity.SUCCESS;
    }

    /**
     * 增加优惠券
     * @param id 用户id
     * @param num 数量
     * @param discountId 优惠券id
     * @return
     */
    @PostMapping("addDis")
    @ResponseBody
    public ResponseEntity<Void> addDis(Integer id,Integer num,Integer discountId){
        Ud ud=new Ud();
        ud.setUserId(id);
        ud.setDiscountId(discountId);

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",discountId);
        Discount discount=discountService.getOne(wrapper);
        Integer day=discount.getExpiryDate();//优惠券有效期
        Calendar calendar = new GregorianCalendar();  // 定义calendar对象
        calendar.setTime(new Date()); // 把当前系统时间赋值给calendar
        calendar.add(calendar.DATE, day); // 在日期中增加天数
        Date date = calendar.getTime(); // 把calendar转换回日期格式
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = ft.format(date);
        ud.setTime(time);

        for(int i=0;i<num;i++){
            udService.save(ud);
        }
        return ResponseEntity.SUCCESS;
    }

    /**
     * 查看用户详情（拥有的优惠券）
     * @param id
     * @return
     */
    @GetMapping("view")
    @ResponseBody
    public ResponseEntity<List<Ud>> view(Integer id){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("userId",id);
        List<Ud> uds=udService.list(wrapper);
        return new ResponseEntity<>(uds);
    }
}
